/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#include "layerlabel.h"
#include "layer.h"
#include "freetrackdialog.h"
#include "ftpushbutton.h"
#include "ftlabel.h"


LayerLabel::LayerLabel(Layer* l) :
    FreeTrackWidget(0),
    layer(l)
{
    specific_data = layer->getLayerLabel();

    desc_label = new FTLabel( layer->getDescription() );
    title_label = new FTLabel( layer->getTitle() );

    edit_button = new FTPushButton( QIcon(":/icons/edit.png"), BUTTON_SIZE);
    connect( edit_button, SIGNAL(clicked()),
             this, SLOT(editLayer()) );

    connect( layer, SIGNAL(titleUpdated(QString)), this, SLOT(liveTitleUpdate(QString)));
    connect( layer, SIGNAL(descriptionUpdated(QString)), this, SLOT(liveDescUpdate(QString)));

    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );

    showEvent(NULL);
}

void LayerLabel::paintEvent(QPaintEvent * pe )
{
    QPainter painter(this);
    painter.drawRect( rect().adjusted(0,0,-1,-1) );
}

void LayerLabel::liveTitleUpdate(QString title)
{
    title_label->setText( title );
}

void LayerLabel::liveDescUpdate( QString desc )
{
    desc_label->setText( desc );
}

void LayerLabel::editLayer()
{
    FreeTrackDialog* dialog = new FreeTrackDialog( layer->getConfigureLayer() );
    dialog->exec();
    delete dialog;
}

QLayout* LayerLabel::createHorizontal()
{
    QHBoxLayout* grid = new QHBoxLayout();

    QVBoxLayout* dataGrid = new QVBoxLayout();
    dataGrid->addWidget( title_label );
    dataGrid->addWidget( desc_label );
    if ( specific_data )
        dataGrid->addWidget( specific_data );
    grid->addLayout( dataGrid );

    grid->addWidget( edit_button );

    return grid;
}

QLayout* LayerLabel::createVertical()
{
    QVBoxLayout* grid = new QVBoxLayout();
    grid->addWidget( title_label );
    grid->addWidget( desc_label );

    QHBoxLayout* udGrid = new QHBoxLayout();
    udGrid->addStretch();
    if ( specific_data )
        udGrid->addWidget( specific_data, 2 );
    udGrid->addWidget( edit_button );

    grid->addLayout( udGrid );

    return grid;
}
